/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor.controller;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import offset.nodes.Constants;
import offset.nodes.client.dialog.newnode.model.NewFile;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.editor.Editor;
import offset.nodes.client.editor.model.ImageModel;
import offset.nodes.client.editor.model.Range;
import offset.nodes.client.editor.view.image.ImageDialog;
import offset.nodes.client.editor.view.image.LocalFileInfo;

/**
 *
 * @author Walter Lütgenau
 */
public class ImageActions {

    public static abstract class AbstractImageAction extends EditorActions.AbstractEditorAction {

        public AbstractImageAction(String name, Editor editor) {
            super(name, editor);
        }

        protected ImageModel getModel() {
            return new ImageModel(getDocumentContext(), editor);
        }
    }

    public static class InsertImageAction extends AbstractImageAction {

        public InsertImageAction(Editor editor) {
            super(java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("insert.image"), editor);
            setIcon(new javax.swing.ImageIcon(getClass().getResource("/offset/nodes/client/editor/view/resources/sc_image.png")));
        }

        protected String uploadImage(LocalFileInfo info) {
            NewFile.Request request = new NewFile.Request();

            String path = (String) editor.getProperties().get(Editor.PROP_DOCUMENT_PATH);
            request.setPath(path + Constants.PATH_SEPARATOR + Constants.CHILD_IMAGES + Constants.PATH_SEPARATOR + info.getName());
            request.setContentType((String) info.getMimeType());
            File contentFile = new File(info.getPath());
            if (!contentFile.exists())
                return null;
            byte[] buf = new byte[(int) contentFile.length()];
            try {
                FileInputStream in = new FileInputStream(contentFile);
                in.read(buf);
                in.close();
                request.setContent(buf);
                request.setCommit(false);

                String upload = (String) editor.getProperties().get(Editor.PROP_UPLOAD_TO);
                ServerModel model = new ServerModel(new URL(upload));
                NewFile.Response response = (NewFile.Response) model.sendRequest(request);

                return response.getUuid();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }

        }

        @Override
        public void action(ActionEvent e) throws Exception {
            ImageDialog dialog = new ImageDialog(editor);
            dialog.setLocation(100, 100);
            if (dialog.showDialog() == ImageDialog.CANCEL)
                return;

            switch (dialog.getImageType()) {
                case ImageDialog.TYPE_REPOSITORY:
                    getModel().insertRepositoryImage(new Range(editor.getBrowserPane().getSelectionStart(),
                            editor.getBrowserPane().getSelectionEnd()), dialog.getRepositoryUuid());
                    break;

                case ImageDialog.TYPE_LOCAL:
                    LocalFileInfo localFile = dialog.getLocalFile();
                    String imageUuid = uploadImage(localFile);
                    getModel().insertLocalImage(new Range(editor.getBrowserPane().getSelectionStart(),
                            editor.getBrowserPane().getSelectionEnd()), imageUuid);
                    break;
            }
        }
    }
}
